package com.lcz.authority.admin.controller.system;

import com.lcz.authority.common.annotation.ApiResource;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.R;
import com.lcz.authority.common.domain.entity.SysConfigEntity;
import com.lcz.authority.common.enums.AppCodeEnum;
import com.lcz.authority.common.enums.ResBizTypeEnum;
import com.lcz.authority.common.service.SysConfigService;
import com.lcz.authority.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lcz
 * @desc 参数配置管理
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@RestController
@RequestMapping("/system/config")
@ApiResource(name = "参数配置管理", resBizType = ResBizTypeEnum.SYSTEM, appCode = AppCodeEnum.SYSTEMMODULES)
@Api(tags = "参数配置管理")
public class SysConfigController {
    @Autowired
    private SysConfigService configService;

    @GetMapping(value = "page", name = "参数配置管理-分页")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @ApiOperation("分页")
    public R page(SysConfigEntity sysConfigEntity) {
        PageResult<SysConfigEntity> page = configService.page(sysConfigEntity);

        return R.ok().put(page);
    }

    @GetMapping(value = "{id}", name = "参数配置管理-查询id信息")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @ApiOperation("查询id信息")
    public R getInfo(@PathVariable("id") String id) {
        SysConfigEntity entity = configService.selectConfigById(id);
        return R.ok().put(entity);
    }

    @GetMapping(value = "/configKey/{configKey:.+}", name = "参数配置管理-查询参数值")
    @ApiOperation("根据参数键名查询参数值")
    public R getConfigKey(@PathVariable String configKey) {
        return R.ok(configService.selectConfigByKey(configKey));
    }

    @PostMapping(name = "参数配置管理-新增")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @ApiOperation("新增")
    public R add(@Validated @RequestBody SysConfigEntity config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return R.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return R.ok(configService.insertConfig(config));
    }

    @PutMapping(name = "参数配置管理-修改")
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @ApiOperation("修改")
    public R edit(@Validated @RequestBody SysConfigEntity config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return R.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return R.ok(configService.updateConfig(config));

    }

    @DeleteMapping(value = "/{configIds}", name = "参数配置管理-删除")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @ApiOperation("删除")
    public R remove(@PathVariable String[] configIds) {
        configService.deleteConfigByIds(configIds);

        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @DeleteMapping(value = "/refreshCache", name = "参数配置管理-刷新缓存")
    @ApiOperation("刷新缓存")
    public R refreshCache() {
        configService.resetConfigCache();
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/export", name = "参数配置管理-导出")
    @ApiOperation("导出参数配置管理数据")
    public void export(HttpServletResponse response, SysConfigEntity sysConfigEntity) {
        List<SysConfigEntity> list = configService.page(sysConfigEntity).getRows();
        ExcelUtil<SysConfigEntity> util = new ExcelUtil<>(SysConfigEntity.class);
        util.exportExcel(response, list, "参数配置管理");
    }
}
